using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.DirectoryServices;
using System.Linq;
using System.Runtime.CompilerServices;
using Landscape.Business.Common;
using System.Web.Security;

namespace Landscape.Business.Interface.LDAP
{
	public sealed class ActiveDirectoryManager
	{
		private DirectoryEntry _directoryEntry;

		private string LDAPPassword
		{
			get
			{
				return ConfigurationManager.AppSettings["LDAPPassword"];
			}
		}

		private string LDAPPath
		{
			get
			{
				return ConfigurationManager.AppSettings["LDAPPath"];
			}
		}

		private string LDAPUser
		{
			get
			{
				return ConfigurationManager.AppSettings["LDAPUserName"];
			}
		}

		private DirectoryEntry SearchRoot
		{
			get
			{
				if (this._directoryEntry == null)
				{
					this._directoryEntry = new DirectoryEntry(this.LDAPPath, this.LDAPUser, this.LDAPPassword, AuthenticationTypes.Secure);
				}
				return this._directoryEntry;
			}
		}

		public ActiveDirectoryManager()
		{
		}

		public List<DirectoryUser> GetActiveDirectoryUsers()
		{
			List<DirectoryUser> directoryUserList = new List<DirectoryUser>();
			try
			{
				this._directoryEntry = null;
				DirectorySearcher directorySearch = new DirectorySearcher(this.SearchRoot);
				foreach (SearchResult sr in directorySearch.FindAll())
				{
					try
					{
						DirectoryEntry directoryEntry = new DirectoryEntry(sr.Path, this.LDAPUser, this.LDAPPassword);
						if (!directoryEntry.SchemaClassName.ToLower().Trim().Equals("user"))
						{
							foreach (DirectoryEntry child in directoryEntry.Children)
							{
								if (!directoryEntry.SchemaClassName.ToLower().Trim().Equals("user"))
								{
									continue;
								}
								List<DirectoryUser> directoryUsers = directoryUserList;
								if (directoryUsers.Select<DirectoryUser, bool>((DirectoryUser exp) => exp.GUID == child.Guid).Count<bool>() != 0)
								{
									continue;
								}
								directoryUserList.Add(new DirectoryUser(child));
							}
						}
						else
						{
							List<DirectoryUser> directoryUsers1 = directoryUserList;
							if (directoryUsers1.Select<DirectoryUser, bool>((DirectoryUser exp) => exp.GUID == directoryEntry.Guid).Count<bool>() == 0)
							{
								directoryUserList.Add(new DirectoryUser(directoryEntry));
							}
						}
					}
					catch (Exception exception)
					{
                        Landscape.Business.Common.Utils.LogMyError(this.GetType(), "An exception occered while trying to get the list of AD users.", string.Empty, exception);
					}
				}
			}
			catch (Exception exception1)
			{
                Landscape.Business.Common.Utils.LogMyError(this.GetType(), "An exception occered while trying to get the list of AD users.", string.Empty, exception1);
			}
			return directoryUserList;
		}

		public List<DirectoryUser> GetActiveDirectoryUsers(string pLikeACName)
		{
			List<DirectoryUser> directoryUserList = new List<DirectoryUser>();
			try
			{
				this._directoryEntry = null;
				DirectorySearcher directorySearch = new DirectorySearcher(this.SearchRoot);
				directorySearch.Filter = string.Concat("(&(objectClass=user)(SAMAccountName=*", pLikeACName, "*))");
				SearchResultCollection results = directorySearch.FindAll();
				if (results != null)
				{
					foreach (SearchResult sr in results)
					{
						DirectoryEntry user = new DirectoryEntry(sr.Path, this.LDAPUser, this.LDAPPassword);
						directoryUserList.Add(new DirectoryUser(user));
					}
				}
			}
			catch (Exception exception)
			{
				Exception ex = exception;
                Landscape.Business.Common.Utils.LogMyError(this.GetType(), string.Format("An exception occered while trying to get the list of AD users like '{0}'.", pLikeACName), string.Empty, ex);
			}
			return directoryUserList;
		}

		public DirectoryUser GetUserByLoginName(string pUserName)
		{
			DirectoryUser directoryUser;
			try
			{
				this._directoryEntry = null;
				DirectorySearcher directorySearch = new DirectorySearcher(this.SearchRoot);
				directorySearch.Filter = string.Concat("(&(objectClass=user)(SAMAccountName=", pUserName, "))");
				SearchResult results = directorySearch.FindOne();
				if (results == null)
				{
					directoryUser = null;
				}
				else
				{
					DirectoryEntry user = new DirectoryEntry(results.Path, this.LDAPUser, this.LDAPPassword);
					directoryUser = new DirectoryUser(user);
				}
			}
			catch (Exception exception)
			{
				Exception ex = exception;
                Landscape.Business.Common.Utils.LogMyError(this.GetType(), string.Format("An exception occered while trying to get the specific AD user '{0}'.", pUserName), string.Empty, ex);
				directoryUser = null;
			}
			return directoryUser;
		}

		public bool ValidateUser(string pUserName, string pPassword)
		{
            return true;

			ActiveDirectoryMembershipProvider adPro = new ActiveDirectoryMembershipProvider();
			NameValueCollection nvc = new NameValueCollection();
			nvc.Add("attributeMapUsername", "sAMAccountName");
			nvc.Add("connectionStringName", "ADConnectionString");
			nvc.Add("connectionUsername", ConfigurationManager.AppSettings["LDAPUserName"]);
			nvc.Add("connectionPassword", ConfigurationManager.AppSettings["LDAPPassword"]);
			adPro.Initialize("MyADMembershipProvider", nvc);
			if (!adPro.ValidateUser(pUserName, pPassword))
			{
				return false;
			}
			MembershipUser user = adPro.GetUser(pUserName, false);
			if (user.IsApproved && !user.IsLockedOut)
			{
				return true;
			}
			return false;
		}
	}
}